
/**    
 * <p>Copyright： 东软烟草产品研发团队</p>   
 */

package com.jkl.service.user;

import java.util.ArrayList;
import java.util.List;

import com.jkl.entity.button.BsButton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jkl.dao.area.BSAreaMapper;
import com.jkl.dao.menu.BsMenuMapper;
import com.jkl.dao.person.BsPersonMapper;
import com.jkl.dao.role.BsRoleMapper;
import com.jkl.dao.user.BSUserMapper;
import com.jkl.entity.login.vo.PJLog;
import com.jkl.entity.login.weight.Weight;
import com.jkl.entity.po.user.BSUser;
import com.jkl.entity.project.po.SljptLog;
import com.jkl.service.user.inter.BSUserServiceInterface;
import com.jkl.untils.MenuUntil;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 123service
 * <p>
 * <p>
 * 123service
 * <p>
 * <p>
 * 变更说明：ji.k 2018年1月17日 123service
 * <p>
 * Date:2018年09月26日
 * 
 * @author ji.k
 * @version 1.0
 */
@Slf4j
@Service
public class BSUserService implements BSUserServiceInterface {

	@Autowired
	private BSUserMapper bSUserMapper;
	@Autowired
	private BsRoleMapper bsRoleMapper;
	@Autowired
	private BsMenuMapper bsMenuMapper;
	@Autowired
	private BSAreaMapper bSAreaMapper;
	@Autowired
	private BsPersonMapper bSPersonMapper;

	@Override
	public BSUser findByUserName(String userName, String passWord) {
		BSUser user = bSUserMapper.findByUserName(userName, passWord);
		List<BsButton> bsButtonList = new ArrayList<>();
		if (user != null) {
			user.setRoles(bSUserMapper.getRoles(user.getRowId()));
			for (int i = 0; i < user.getRoles().size(); i++) {
				user.getRoles().get(i)
						.setMenus(MenuUntil.assembleMenu(bSUserMapper.getMenus(user.getRoles().get(i).getRowId())));

				bsButtonList.addAll(bSUserMapper.getButtonMenus(user.getRoles().get(i).getRoleCode()));
			}
			user.setButtons(bsButtonList);
			user.setBsArea(bSAreaMapper.selectByPrimaryKey(user.getAreaId()));
			user.setBsPerson(bSPersonMapper.selectByPrimaryKey(user.getPersonId()));

			return user;
		} else {
			return null;
		}
	}

	@Override
	public PageInfo<BSUser> getAllUserByParam(BSUser params) {
		PageHelper.startPage(params.getCurrentPage(), params.getPageSize());
		return new PageInfo<>(bSUserMapper.getAllUserByParam(params));
	}

	@Override
	public boolean addUserRole(BSUser bsUser) {
		try {
			bSUserMapper.addUserRole(bsUser.getRowId(), bsUser.getRoles().get(0).getRowId());
			return true;
		} catch (Exception e) {
			log.error(e.getMessage());
			return false;
		}
	}

	@Override
	public void updateUser(BSUser bsUser) {
		bSUserMapper.updateByPrimaryKey(bsUser);
	}

	@Override
	public void addUser(BSUser bsUser) {
		bsUser.setDeptName(bSPersonMapper.getdeptNameById(bsUser).get(0));
		bSUserMapper.insert(bsUser);
	}

	@Override
	public void addLog(PJLog pjlog) {
		bsMenuMapper.addLog(pjlog);
	}

	@Override
	public PageInfo<PJLog> getLogList(PJLog param) {
		PageHelper.startPage(param.getCurrentPage(), param.getPageSize());
		List<PJLog> list = bsMenuMapper.getLogList(param);
		PageInfo<PJLog> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}
	//@Override
	//public Weight queryWeight(int rowId) {
	//	return bSUserMapper.queryWeight(rowId);
	//}
	@Override
	public int insertWeight(Weight weight) {
		return bSUserMapper.insertWeight(weight);
	}

	@Override
	public boolean addUserWithPersonId(Integer rowId,Integer personId) {
		return bSUserMapper.addUserWithPersonId(rowId,personId);
	}

	// 修改密码
	@Override
	public int updatePassword(int rowId, String toMD5) {

		return bsMenuMapper.updatePassword(rowId, toMD5);
	}

    @Override
    public String getPasswordState(String rowId) {
		return bsMenuMapper.getPasswordState(rowId);
    }













    //@Override
	//public int updateWeight(Weight weight) {
	//	return bSUserMapper.updateWeight(weight);
	//}
}